package top.dbpanda.cmsjpa.learn.thread.juc.pool;

import java.util.ArrayList;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

/**
 * @author 20018954
 */
public class TestTreadPoolWithRunable {
    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(6);
        ArrayList<Future<Integer>> futures = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            //使用Callable()
            Future<Integer> future = pool.submit(() -> {
                int sum = 0;
                for (int j = 0; j <= 10; j++) {
                    sum += j;
                }
                return sum;
            });
            //在submit()返回结果前处于阻塞状态
            futures.add(future);
        }
        futures.forEach(
                item -> {
                    try {
                        System.out.println(item.get());
                    } catch (InterruptedException | ExecutionException e) {
                        e.printStackTrace();
                    }
                }
        );
        //关闭线程池
        pool.shutdown();
    }
}
